package com.local.ApacheCommonsTools.Compress;

import java.io.File;
import java.io.FileInputStream;

import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
import org.joda.time.DateTime;
import org.joda.time.Period;
/**
 * Apache Commons Compress software defines an API for working with compression and archive formats. 
 * These include: bzip2, gzip, pack200, lzma, xz, Snappy,
 *  traditional Unix Compress, DEFLATE and ar, cpio, jar, tar, zip, dump, 7z, arj.
 */
public class CompressDemo {
	public static void main(String[] args) throws Exception {
		//创建压缩对象  
	    ZipArchiveEntry entry = new ZipArchiveEntry("Jfinal.pdf");  
	      //要压缩的文件  
	      File f=new File("F:\\Jfinal.pdf");  
	      if(!f.exists()) {
	    	  throw new Exception("文件不存在");
	      }
	      FileInputStream fis=new FileInputStream(f);  
	      System.out.println("开始压缩！");
	      DateTime start = new DateTime();
	      ZipArchiveOutputStream zipOutput=new ZipArchiveOutputStream(new File("F:\\Jfinal.rar"));    
	      zipOutput.putArchiveEntry(entry);  
	      int i=0,j;  
	      while((j=fis.read()) != -1)  
	      {   
	       zipOutput.write(j);  
	       i++;  
	      // System.out.println(i);  
	      }  
	      zipOutput.closeArchiveEntry();  
	      zipOutput.close();  
	      fis.close();  
	      DateTime end = new DateTime(); 
	      Period period=new Period(start,end);
	      String spendtime = period.getHours()+"时"+period.getMinutes()+"分"+period.getSeconds()+"秒";
	      System.out.println("执行完了！耗时:"+spendtime);
	}
}
